package com.example.kehoobackend.controller;

import com.example.kehoobackend.common.CommonResponse;
import com.example.kehoobackend.domain.User;
import com.example.kehoobackend.service.UserService;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Validated
@CrossOrigin
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @return CommonResponse<Map>
     */
    @GetMapping("/user")
    public CommonResponse<Map> login(
            @RequestParam("username") @NotBlank(message = "用户名不能为空") String username,
            @RequestParam("password") @NotBlank(message = "密码不能为空") String password) {
        return userService.login(username, password);
    }

    /**
     * 查询用户信息
     *
     * @param user_id 用户id
     * @return CommonResponse<User>
     */
    @GetMapping("/user/{user_id}/info")
    public CommonResponse<User> getUserInfo(
            @PathVariable("user_id") Integer user_id) {
        return userService.getUserInfo(user_id);
    }

    /**
     * 根据用户ID返回token和用户信息（刷脸登录）
     *
     * @param user_id 用户id
     * @return CommonResponse<Map>
     */
    @GetMapping("/user/{user_id}/face")
    public CommonResponse<Map> faceLogin(
            @PathVariable("user_id") Integer user_id) {
        return userService.faceLogin(user_id);
    }

    /**
     * 查询有无重名用户
     *
     * @param username 用户名
     * @return CommonResponse<Boolean>
     */
    @GetMapping("/user/username")
    public CommonResponse<Boolean> detectSameUsername(
            @RequestParam("username") @NotBlank(message = "查询的用户名不能为空") String username) {
        return userService.usernameIsExist(username)
                ? CommonResponse.createForSuccess("用户不存在", true)
                : CommonResponse.createForSuccess("用户存在", false);
    }

    /**
     * 查询有无相同邮箱
     *
     * @param email 邮箱
     * @return CommonResponse<Boolean>
     */
    @GetMapping("/user/email")
    public CommonResponse<Boolean> detectSameEmail(
            @RequestParam("email") @NotBlank(message = "查询的邮箱不能为空") String email) {
        return userService.emailIsExist(email)
                ? CommonResponse.createForSuccess("邮箱不存在", true)
                : CommonResponse.createForSuccess("邮箱存在", false);
    }

    /**
     * 推荐用户
     *
     * @param user_id 用户id
     * @return CommonResponse<List < User>>
     */
    @GetMapping("/user/recommend")
    public CommonResponse<List<User>> recommendUser(
            @RequestParam("user_id") Integer user_id) {
        return userService.recommendUser(user_id);
    }

    /**
     * 用户模糊搜索
     *
     * @param user_id 用户id
     * @param keyword 关键词
     * @return CommonResponse<List < User>>
     */
    @GetMapping("/user/keyword")
    public CommonResponse<List<User>> searchUser(
            @RequestParam("keyword") @NotBlank(message = "关键词不能为空") String keyword,
            @RequestParam("user_id") Integer user_id) {
        return userService.searchUser(keyword, user_id);
    }

    /**
     * 判断用户密码是否正确
     *
     * @param user_id 用户id
     * @param password 密码
     * @return CommonResponse<Boolean>
     */
    @GetMapping("/user/password")
    public CommonResponse<Boolean> checkPassword(
            @RequestParam("user_id") Integer user_id,
            @RequestParam("password") @NotBlank(message = "密码不能为空") String password) {
        return userService.verifyPassword(user_id, password)
                ? CommonResponse.createForSuccess("密码正确", true)
                : CommonResponse.createForSuccess("密码错误", false);
    }




    /**
     * 注册
     *
     * @param user  JSON格式的用户信息
     * @param token 邮箱令牌
     * @param code  邮箱验证码
     * @return CommonResponse<User>
     */
    @PostMapping("/user")
    public CommonResponse<Boolean> regist(
            @RequestBody User user,
            @RequestParam("token") String token,
            @RequestParam("code") String code) {
        return userService.register(user, token, code)
                ? CommonResponse.createForSuccess("注册成功", true)
                : CommonResponse.createForSuccess("注册失败", false);
    }




    /**
     * 修改用户信息
     *
     * @param user JSON格式的用户信息
     * @return CommonResponse<Boolean>
     */
    @PatchMapping("/user")
    public CommonResponse<Boolean> updateUserInfo(
            @RequestBody User user) {
        return userService.updateUserInfo(user)
                ? CommonResponse.createForSuccess("用户信息修改成功", true)
                : CommonResponse.createForSuccess("用户信息修改失败", false);
    }

    /**
     * 使用密码修改密码
     *
     * @param user_id  用户id
     * @param password 新密码
     * @return CommonResponse<Boolean>
     */
    @PatchMapping("/user/password")
    public CommonResponse<Boolean> updatePassword(
            @RequestParam("user_id")  Integer user_id,
            @RequestParam("password") @NotBlank(message = "密码不能为空") String password) {
        return userService.changePassword(user_id, password)
                ? CommonResponse.createForSuccess("密码修改成功", true)
                : CommonResponse.createForSuccess("密码修改失败", false);
    }

    /**
     * 使用邮箱修改密码
     *
     * @param user_id  用户id
     * @param password 邮箱
     * @param token    邮箱令牌
     * @param code     邮箱验证码
     * @return CommonResponse<Boolean>
     */
    @PatchMapping("/user/password/{email}")
    public CommonResponse<Boolean> updatePasswordByEmail(
            @PathVariable("email") @NotBlank(message = "邮箱不能为空") String email,
            @RequestParam("user_id")  Integer user_id,
            @RequestParam("password") @NotBlank(message = "邮箱不能为空") String password,
            @RequestParam("token") @NotBlank(message = "邮箱令牌不能为空") String token,
            @RequestParam("code") @NotBlank(message = "邮箱验证码不能为空") String code) {
        return userService.changePasswordByEmail(user_id, password, email, token, code)
                ? CommonResponse.createForSuccess("密码修改成功", true)
                : CommonResponse.createForSuccess("密码修改失败", false);
    }

    /**
     * 修改邮箱
     *
     * @param user_id 用户id
     * @param email   新邮箱
     * @param token   邮箱令牌
     * @param code    邮箱验证码
     * @return CommonResponse<Boolean>
     */
    @PatchMapping("/user/email")
    public CommonResponse<Boolean> updateEmail(
            @RequestParam("user_id")Integer user_id,
            @RequestParam("email") @NotBlank(message = "邮箱不能为空") String email,
            @RequestParam("token") @NotBlank(message = "邮箱令牌不能为空") String token,
            @RequestParam("code") @NotBlank(message = "邮箱验证码不能为空") String code) {
        return userService.changeEmail(user_id, email, token, code)
                ? CommonResponse.createForSuccess("邮箱修改成功", true)
                : CommonResponse.createForSuccess("邮箱修改失败", false);
    }


    /**
     * 注销账号
     *
     * @param user_id 用户id
     * @return CommonResponse<Boolean>
     */
    @DeleteMapping("/user")
    public CommonResponse<Boolean> deleteUser(
            @RequestParam("user_id") Integer user_id) {
        return userService.deleteUser(user_id)
                ? CommonResponse.createForSuccess("注销成功", true)
                : CommonResponse.createForSuccess("注销失败", false);
    }

    /**
     * 退出登录
     *
     * @param user_id 用户id
     * @return CommonResponse<Boolean>
     */
    @DeleteMapping("/user/token")
    public CommonResponse<Boolean> logout(
            @RequestParam("user_id") Integer user_id) {
        return userService.logout(user_id)
                ? CommonResponse.createForSuccess("退出成功", true)
                : CommonResponse.createForSuccess("退出失败", false);
    }



    //通过用户名获取User(私信开发用方法)
    @GetMapping("/user/{username}")
    public CommonResponse<User> getUserIdByUsername(
            @PathVariable("username") String username){
        return CommonResponse.createForSuccess("由用户名返回用户ID与用户名",userService.getUserByUsername(username));
    }


}
